/*
 * DF.FormSubmitter (for jQuery)
 * version: 1.0 (12/10/2010)
 * @requires jQuery v1.4 + , jQuery.DF v1.0 +
 *
 *
 * Licensed under the MIT License: http://en.wikipedia.org/wiki/MIT_License
 *
 * Copyright (c) 2012 Dmytro Feshchenko www.df-studio.net (info@df-studio.net)
 *
 * Usage:
 *
 *  var options = {
 *		validateCallback: function()
 *		{ *			if(someFieldEmpty)
 *			{ *				return true;
 *			}
 *		}
 *  };
 *  $(document).ready(function(){
 *	  	$('form').df('formSubmitter', options);
 * 	});
 *
 *
 */




;(function($) {

	try
	{
		$.df.formSubmitter = function(params, form){

		    var _this = this;
			var development =
			{
				name:		'DF.FormSubmiter',
				version:	'1.0',
				year:		'2012'
			};
			var options =
			{
				divId: 		'dffsd'+Math.round(Math.random()*100000),
				frameId: 	'dffsf'+Math.round(Math.random()*100000),
				frameSource: 'about:blank',
				submitButtonClassName: 'df-form-submit',
				processSubmitButtonClassName: 'df-form-submit-process',
				submitCallback: function(formSubmitter, data)
				{
					eval('var response = ' + data);
					var r = response.handler.call(this, formSubmitter);
				},
				validateCallback: function(formSubmitterObject){}
			};
			var elems = {};

			this.getElems = function()
			{
				return elems;
			}

			this.getOptions = function()
			{
				return options;
			}

			var init = function(settings, form)
			{
				settings = settings || {};
				options = $.extend(true, options, settings);
				var button = form.find('.' + options.submitButtonClassName);
				elems = $.extend(true, elems, {
					form: form,
					button: button.length ? button : null
				});

				if(elems.button)
				{
					elems.button.bind('click', function(event){

						_this.submit();
					})
				}
			};

			this.submit = function()
			{
				if(typeof(options.validateCallback) == 'function' && options.validateCallback.call(elems.form, this) || elems.button.hasClass(options.processSubmitButtonClassName))
				{
					return false;
				}
				elems.button.addClass(options.processSubmitButtonClassName);
				build();
				elems.frame.bind('load', function(){

					var frame = $(this).contents().get(0);
					try
					{
						var url = frame.URL;
					}
					catch(e)
					{
						var url = options.frameSource;
						alert('ERROR!!! There are no access to iframe document');
					}
					if(url != options.frameSource)
					{
						data = $(frame.body).html();
						destroy();
						elems.button.removeClass(options.processSubmitButtonClassName);
						options.submitCallback.call(elems.form, _this, data);
					}
				});
				elems.form.submit();
			};

			var build = function()
			{
				var divId = options.divId;
				var frameId = options.frameId;
				var frameSource = options.frameSource;
				var form = elems.form;
				var div = $("#"+divId);
				var frame = $('#'+frameId);
				if(!div.length)
				{
					div = form.after('<div/>').next('div:first').attr('id', divId).css('display', 'block');
					frame = div.prepend('<iframe id="'+frameId+'" name="'+frameId+'" src="'+frameSource+'"></iframe>').children('iframe:first').attr({
						width: 1,
						height: 1
					});
				}

				elems = $.extend(true, elems, {
					div:div,
					frame:frame
				});
				form.attr('target', frameId);
			};

			var destroy = function()
			{
				elems.div.remove();
			};

			init(params, form);
		};


		$.fn.df.formSubmitter = function(settings)
		{
			settings = settings || {};
			var forms = [];
			return this.each(function(i){
				form = $(this);
				forms[i] = new $.df.formSubmitter(settings, form);
			});
		};
	}
	catch(e)
	{
		alert("jQuery.df library is undefined. Unable to load DF.FormSubmitter plugin.");
	}

})(jQuery);

